--[[
军团副本逻辑
--]]
local ExcelMap = require("excel.Map")
local CopyConfig = ExcelMap.copy
local SceneConfig = ExcelMap.scene
local Timer = require("core.Timer")
local ObjHuman = require("core.ObjHuman")
local Copy = require("copy.CopyL")
local Loss = require("copy.Loss")
local Aim = require("copy.Aim")
local CopyHandler = require("copy.HandlerL")

function applyCopyRoomFamily(copyID, config, human, startTime)
--	for roomID in pairs(Copy.copyList[copyID]) do
--		if Copy.roomDatas[copyID][roomID] and Copy.roomDatas[copyID][roomID].familyUuid == human.familyUuid then
--			if Copy.roomDatas[copyID][roomID].finishCode then return end -- 军团副本房间已经结束了
--			
--			local member = Copy.roomDatas[copyID][roomID].member
--			member[#member + 1] = human._id 
--			Copy.copyHuman2Room[human._id] = roomID
--			return roomID
--		end
--	end
	
	for roomID in pairs(Copy.copyList[copyID]) do
		-- 全部已开的房间都不属于这个军团 看是否有未分配的房间
		if Copy.roomDatas[copyID][roomID] == nil then
			Copy.roomDatas[copyID][roomID] = {member = {}, onlineNum=0,startTime=startTime or Timer.now, familyUuid = human.familyUuid}
			Copy.roomBegin(copyID, roomID)
			return roomID
		end
	end	
	return nil
end

function isEntered(human, copyID, roomID)
  if Copy.copyList[copyID] == nil then return end
  local members = Copy.roomDatas[copyID][roomID].member
  for index, uuid in ipairs(members) do
    if uuid == human._id then
      return true
    end
  end 
  return nil
end
function humanBeginCopy(human, copyID, roomID,x,y)
	local copyConfig = CopyConfig[copyID]
	local mapID = copyConfig.mapList[1]
	local sceneConfig = SceneConfig[mapID]

  local bEntered = isEntered(human, copyID, roomID)
  if bEntered == true then
    -- 已经进入过了
  else
	   CopyHandler.incCopyEnterCount(human, copyID)

	   Loss.handler(human, copyConfig, copyID)	-- 扣除进入需要的消耗
	   local members = Copy.roomDatas[copyID][roomID].member
     members[#members + 1] = human._id
	end
	
	--if not x then
	--	x = sceneConfig.x
	--end
	
	--if not y then
	--	y = sceneConfig.y
	--end
	
	-- 根据副本目标进度获取进入的地图号
	local mapID, x, y = Aim.getEnterMap(copyID, roomID)
	if mapID == nil then
      mapID = copyConfig.mapList[1]
      local sceneConfig = SceneConfig[mapID]
      x = sceneConfig.x
      y = sceneConfig.y
	end
	
	
	Copy.copyHuman2Room[human._id] = roomID
	
	ObjHuman.enterMap(human, mapID, x, y)
	human.enterCopyTime = Timer.now
end

function humanEndCopy(human)
	local sceneID = human.scene_id
	local copyID = Copy.sceneID2CopyID[sceneID]
	local roomID = Copy.sceneID2Room[sceneID]
	
	local config = CopyConfig[copyID]
	local backHomeMapId, backHomeX, backHomeY = Copy.getBackHomePoint(human, copyID)
	ObjHuman.enterMap(human,backHomeMapId,backHomeX,backHomeY)

	-- 军团副本的话 把这个玩家从roomdata中干掉
	Copy.copyHuman2Room[human._id] = nil
	local members = Copy.roomDatas[copyID][roomID].member 
	for index, uuid in ipairs(members) do
		if uuid == human._id then
			members[index] = members[#members]
			members[#members] = nil
		end
	end
	if #members == 0 then -- 全部玩家都主动推出了 那么这个房间clear
		if config.noManThenFinish == 1 then
			Copy.roomClear(copyID, roomID)
		end
	end
end

function onQuitFamilyCB(uuid, copyID, roomID)
  if Copy.copyHuman2Room[uuid] == roomID then
    Copy.copyHuman2Room[uuid] = nil
  end 
  local human = ObjHuman.onlineUuid[uuid]
  if human == nil then return end 
  -- 如果这个玩家在组队副本中 一离队就要把他踢出去
  local sceneID = human.scene_id
  local humanCopyID = Copy.sceneID2CopyID[sceneID]
  local humanRoomID = Copy.sceneID2Room[sceneID]
  if humanRoomID == roomID and humanCopyID == copyID then
    humanEndCopy(human)   
    local members = Copy.roomDatas[copyID][roomID].member
    for i = 1, #members do
      if members[i] == uuid then
        members[i] = members[#members]
        members[#members] = nil
        break
      end
    end
  end
end